package bom.controller.organization;


import bom.configuration.util.JsonHelper;
import bom.pojo.organization.User;
import bom.service.i.organization.TreeService;
import bom.service.i.organization.UserAssociateRoleService;
import bom.service.i.organization.UserRoleService;
import bom.service.i.organization.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;
import java.util.Optional;

/**
 * @Author: Fancyears·Maylos·Malvis
 * @Description: 用户维护controller
 * @Date: Created in  2018/11/27 15:46
 * @Modified By:
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private TreeService treeService;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private UserAssociateRoleService userAssociateRoleService;

    /**
     * 功能描述：加载首页菜单节点的数据
     *
     * @return
     */
    @RequestMapping(value = "/mainTree", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String, Object> mainTree() {
        return treeService.mainTree();
    }

    /**
     * 功能描述：更新用户状态为禁用/启用
     *
     * @param entity
     * @return
     */
    @RequestMapping(value = "/userControl", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String, Object> userControl(User entity) {
        return userService.userContr(entity);
    }

    /**
     * 功能描述：跳转到更新用户的页面
     *
     * @param entity
     * @param model
     * @return
     * @throws Exception
     */
    public String updatePage(User entity, Model model) throws Exception {
        entity = userService.get(entity);
        entity.setRoleArray(JsonHelper.list2json(Optional.ofNullable(userService.findByLogin(entity.getLogin())).filter(u -> u != null).orElse(new User()).getRoles()));
        model.addAttribute("entity", entity);
        String path = "";
        return path;
    }

    /**
     * 功能描述：加载所有的权限数据
     *
     * @return
     */
    @RequestMapping(value = "/loadRoles", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String, Object> loadRoles() {
        return userRoleService.loadRoles();
    }

    /**
     * 功能描述：跳转到选择组织架构页面
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/pickGroup")
    public String pickGroup() throws Exception {
        return "";
    }


    /**
     * @param entity
     * @return
     * @Author haozt on 2018/10/26
     * 功能描述:重写父类的update方法  这里进行权限判断
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String, Object> update(User entity) throws Exception {
        return userRoleService.update(entity);
    }


    /**
     * 功能描述：保存数据字典数据
     *
     * @param entity
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String, Object> save(User entity) throws Exception {
        return userService.saveUser(entity);
    }


    /**
     * 功能描述：删除用户
     *
     * @param json
     * @return
     */
    @RequestMapping(value = "/removeBath", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String, Object> removeBath(String json) throws Exception {
        return userService.removeByBatch(json);
    }
}
